package com.moli.iov.admin.web.controller;

import com.moli.iov.admin.model.vo.req.SoftVersionReqVo;
import com.moli.iov.admin.model.vo.req.TboxUpgradeFileReqVo;
import com.moli.iov.admin.model.vo.req.UpgradeFileQueryVo;
import com.moli.iov.admin.service.FtpService;
import com.moli.iov.admin.service.TboxUpgradeFileService;
import com.moli.iov.base.model.dto.req.SoftVersionReqDto;
import com.moli.iov.base.model.dto.req.TboxUpgradeFileReqDto;
import com.moli.iov.base.model.dto.req.UpgradeFileQueryDto;
import com.moli.iov.base.model.dto.res.TboxUpgradeFileResDto;
import com.moli.iov.down.remote.IVehicleParamRemoteService;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * The type Tbox upgrade file controller.
 *
 * @author wanghualin
 * @date 2019-10-09 20:26:21
 */
@Slf4j
@RestController
@RequestMapping("/sys/tboxUpgradeFile")
public class TboxUpgradeFileController extends BaseController {
    @Autowired
    private TboxUpgradeFileService tboxUpgradeFileService;

    @Autowired
    private FtpService ftpService;

    @Reference(group = "${service.group}", check = false)
    private IVehicleParamRemoteService vehicleParamRemoteService;

    /**
     * 系统设置参数列表
     */
    @PostMapping("/list")
    public RestResponse listTboxUpgradeFile(
            @Validated() @RequestBody UpgradeFileQueryVo vo) {
        assembleUserInfo(vo);
        UpgradeFileQueryDto dto = new UpgradeFileQueryDto();
        CommonUtils.copyProperties(vo, dto);
        RestResponse<PageExtend<TboxUpgradeFileResDto>> iPageRestResponse = tboxUpgradeFileService
                .listTboxUpgradeFile(dto);
        return iPageRestResponse;
    }

    /**
     * 添加系统设置参数
     */
    @PostMapping("/save")
    public RestResponse saveTboxUpgradeFile(
            @Validated({TboxUpgradeFileReqVo.Save.class}) @RequestBody TboxUpgradeFileReqVo vo) {
        assembleUserInfo(vo);
        TboxUpgradeFileReqDto dto = new TboxUpgradeFileReqDto();
        CommonUtils.copyProperties(vo, dto);
        dto.setCompanyId(vo.getCompanyId());
        return tboxUpgradeFileService.saveUpgradeFile(dto);
    }

    /**
     * update系统设置参数
     */
    @PostMapping("/update")
    public RestResponse updateTboxUpgradeFile(
            @Validated({TboxUpgradeFileReqVo.Update.class}) @RequestBody TboxUpgradeFileReqVo vo) {
        assembleUserInfo(vo);
        TboxUpgradeFileReqDto dto = new TboxUpgradeFileReqDto();
        CommonUtils.copyProperties(vo, dto);
        return tboxUpgradeFileService.updateUpgradeFile(dto);
    }

    /**
     * delete系统设置参数
     */
    @PostMapping("/delete")
    public RestResponse deleteTboxUpgradeFile(
            @Validated({TboxUpgradeFileReqVo.Delete.class}) @RequestBody TboxUpgradeFileReqVo vo) {
        assembleUserInfo(vo);
        TboxUpgradeFileReqDto dto = new TboxUpgradeFileReqDto();
        CommonUtils.copyProperties(vo, dto);
        return tboxUpgradeFileService.deleteUpgradeFile(dto);
    }

    /**
     * delete系统设置参数
     */
    @PostMapping("/details")
    public RestResponse detailsTboxUpgradeFile(
            @Validated({TboxUpgradeFileReqVo.Delete.class}) @RequestBody TboxUpgradeFileReqVo vo) {
        assembleUserInfo(vo);
        TboxUpgradeFileReqDto dto = new TboxUpgradeFileReqDto();
        CommonUtils.copyProperties(vo, dto);

//
        RestResponse<TboxUpgradeFileResDto> res = tboxUpgradeFileService.getTboxUpgradeFileDetails(dto);
        if (res != null && res.getData() != null && !StringUtils.isEmpty(res.getData().getFileUrl())) {
            TboxUpgradeFileResDto result = res.getData();
            String fileUrl = result.getFileUrl().substring((result.getFileUrl().lastIndexOf("/") + 1));
            result.setFileUrl(fileUrl);
            res.setData(result);
        }
        return res;
    }

    @RequestMapping("/upload")
    public RestResponse upload(@RequestParam("file") MultipartFile multipartFile) {
        TboxUpgradeFileReqVo vo = new TboxUpgradeFileReqVo();
        RestResponse res = ftpService.upload(multipartFile, vo);
        return res;
    }

    @RequestMapping("/softVersion/list")
    public RestResponse softVersionList(@RequestBody SoftVersionReqVo vo) {
        log.info("查询软件版本列表：{}", (vo == null ? "参数为null" : vo.toString()));
        assembleUserInfo(vo);
        SoftVersionReqDto reqDto = new SoftVersionReqDto();
        CommonUtils.copyProperties(vo, reqDto);
        try {
            if (vo == null || vo.getTboxModel() == null) {
                Set<String> list = vehicleParamRemoteService.getSoftVersionList();
                List<TboxUpgradeFileResDto> res = new ArrayList<>();
                if (list != null && list.size() > 0) {
                    List<String> versionlist = new ArrayList<>(list);
                    Collections.sort(versionlist);
                    for (String s : versionlist) {
                        TboxUpgradeFileResDto dto = new TboxUpgradeFileResDto();
                        dto.setSoftwareVersion(s);
                        res.add(dto);
                    }
                }
                return RestResponse.success(res);
            }
            RestResponse res = tboxUpgradeFileService.softVersionList(reqDto);
            return res;
        } catch (Exception e) {
            log.error("获取软件列表失败,{}", e);
            return RestResponse.failure("查询服务失败");
        }
    }
//  log.info("查询软件版本列表：{}",(vo==null?"参数为null":vo.toString()));
//  assembleUserInfo(vo);
//  SoftVersionReqDto reqDto = new SoftVersionReqDto();
//    CommonUtils.copyProperties(vo, reqDto);
//    try{
//    Set<String> list = vehicleParamRemoteService.getSoftVersionList();
//    List<TboxUpgradeFileResDto> res = new ArrayList<>();
//    if(list!=null && list.size()>0){
//      List<String> versionlist = new ArrayList<>(list);
//      Collections.sort(versionlist);
//      for (String s : versionlist) {
//        TboxUpgradeFileResDto dto = new TboxUpgradeFileResDto();
//        dto.setSoftwareVersion(s);
//        res.add(dto);
//      }
//    }
//    return RestResponse.success(res);
//  }catch (Exception e){
//    log.error("获取软件列表失败,{}",e);
//    return RestResponse.failure("查询服务失败");
//  }
}
